import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:merchants/models/wechat_user_info.dart';
import 'package:merchants/services/category.dart';
import 'package:merchants/services/common/upload.dart';
import 'package:merchants/services/join_merchant_service.dart';
import 'package:merchants/services/merchant.dart';
import 'package:merchants/services/merchant_secret.dart';
import 'package:merchants/services/merchant_user.dart';
import 'package:merchants/services/sku.dart';
import 'package:merchants/services/spu.dart';
import 'package:merchants/services/we_chat_service.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:merchants/services/role_service.dart';
import 'package:merchants/services/permission_service.dart';
import 'package:merchants/services/attr_name.dart';
import 'package:merchants/services/attr_value.dart';

final sharedPreferencesProvider = FutureProvider<SharedPreferences>((
  ref,
) async {
  return await SharedPreferences.getInstance();
});

final authProvider = ChangeNotifierProvider<AuthProvider>((ref) {
  final sharedPrefs = ref
      .watch(sharedPreferencesProvider)
      .maybeWhen(data: (prefs) => prefs, orElse: () => null);
  return AuthProvider(sharedPrefs);
});

class AuthProvider with ChangeNotifier {
  WeChatUserInfo? _userInfo;
  String? _merchantId;
  String? _cloudFunctionUrl;
  final SharedPreferences? _sharedPrefs;

  AuthProvider(this._sharedPrefs) {
    _loadInitialData();
  }

  void _loadInitialData() {
    _userInfo = WeChatUserInfo.fromSharedPreferences(
      _sharedPrefs?.getString('userInfo'),
    );
    _merchantId = _sharedPrefs?.getString('merchantId');
    _cloudFunctionUrl = _sharedPrefs?.getString('cloudFunctionUrl');
  }

  WeChatUserInfo? get userInfo => _userInfo;

  String? get merchantId => _merchantId;

  String? get cloudFunctionUrl => _cloudFunctionUrl;

  void setUserInfo(WeChatUserInfo userInfo) {
    _userInfo = userInfo;
    _sharedPrefs?.setString('userInfo', userInfo.toSharedPreferences());
    notifyListeners();
  }

  void setMerchantId(String merchantId) {
    _merchantId = merchantId;
    _sharedPrefs?.setString('merchantId', merchantId);
    notifyListeners();
  }

  void setMerchantCloudFunctionUrl(String url) {
    _cloudFunctionUrl = url;
    _sharedPrefs?.setString('cloudFunctionUrl', url);
    notifyListeners();
  }

  void clear() {
    _userInfo = null;
    _merchantId = null;
    _cloudFunctionUrl = null;
    _sharedPrefs?.remove('userInfo');
    _sharedPrefs?.remove('merchantId');
    _sharedPrefs?.remove('cloudFunctionUrl');
    notifyListeners();
  }

  bool hasScope(String scope) {
    return _userInfo?.scopes?.contains(scope) ?? false;
  }
}

final merchantServiceProvider = Provider<MerchantService>((ref) {
  return MerchantService(ref: ref);
});

final merchantUserServiceProvider = Provider<MerchantUserService>((ref) {
  return MerchantUserService(ref: ref);
});

final weChatServiceProvider = Provider<WeChatService>((ref) {
  return WeChatService(ref: ref);
});

final spuServiceProvider = Provider<SpuService>((ref) {
  return SpuService(ref: ref);
});

final skuServiceProvider = Provider<SkuService>((ref) {
  return SkuService(ref: ref);
});

final categoryServiceProvider = Provider<CategoryService>((ref) {
  return CategoryService(ref: ref);
});

final commonUploadServiceProvider = Provider<CommonUploadService>((ref) {
  return CommonUploadService(ref: ref);
});

final joinMerchantServiceProvider = Provider<JoinMerchantService>((ref) {
  return JoinMerchantService(ref: ref);
});

final merchantSecretServiceProvider = Provider<MerchantSecretService>((ref) {
  return MerchantSecretService(ref: ref);
});

// ─────────── 角色 / 权限 ───────────
final roleServiceProvider = Provider<RoleService>((ref) {
  return RoleService(ref: ref);
});

final permissionServiceProvider = Provider<PermissionService>((ref) {
  return PermissionService(ref: ref);
});

final attrNameServiceProvider = Provider<AttrNameService>((ref) {
  return AttrNameService(ref: ref);
});

final attrValueServiceProvider = Provider<AttrValueService>((ref) {
  return AttrValueService(ref: ref);
});

